package com.fan.configService.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fan.configService.exception.QhException;
import com.fan.configService.exception.QhUserErrors;
import com.fan.configService.mapper.ApplicationItemMapper;
import com.fan.configService.po.ApplicationEnv;
import com.fan.configService.po.ApplicationItem;
import com.fan.configService.util.ConvertUtil;
import com.fan.configService.controller.Request.ApplicationItemRequest;
import com.fan.configService.vo.ApplicationItemVo;
import com.fan.configService.vo.MyPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/9/3 10:41
 * @UpdateUser: hf
 * @UpdateDate: 2018/9/3 10:41
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Service
public class ApplicationItemService {
    @Autowired
    ApplicationItemMapper applicationItemMapper;
    @Autowired
    ApplicationEnvService applicationEnvService;

    public ApplicationItemVo getApplicationItemByName(String itemName) {
        LambdaQueryWrapper<ApplicationItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ApplicationItem::getItemName, itemName);
        ApplicationItem applicationItem = applicationItemMapper.selectOne(lambdaQueryWrapper);
        if (applicationItem == null) {
            throw new QhException(QhUserErrors.APPLICATION_NOT_EXIST);
        }
        ApplicationItemVo applicationItemVo = new ApplicationItemVo();
        BeanUtils.copyProperties(applicationItem, applicationItemVo);
        return applicationItemVo;
    }

    public void addItem(ApplicationItemRequest applicationItemRequest) {
        ApplicationItem applicationItem = new ApplicationItem();
        BeanUtils.copyProperties(applicationItemRequest, applicationItem);
        applicationItemMapper.insert(applicationItem);
    }


    public MyPage<ApplicationItemVo> getApplicationEnvItem(Long applicationId, Long applicationEnvId, MyPage myPage) {
        Page page = new Page();
        BeanUtils.copyProperties(myPage, page);
        LambdaQueryWrapper<ApplicationItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ApplicationItem::getApplicationEnvId, applicationEnvId);
        IPage<ApplicationItem> iPage = applicationItemMapper.selectPage(page, lambdaQueryWrapper);
        List<ApplicationItemVo> applicationItemVoList = ConvertUtil.copyTo(iPage.getRecords(), ApplicationItemVo.class);
        myPage.setTotal(iPage.getTotal());
        myPage.setRecords(applicationItemVoList);
        return myPage;
    }

    public List<ApplicationItem> getApplicationEnvItem(Long applicationId, Long applicationEnvId) {
        LambdaQueryWrapper<ApplicationItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ApplicationItem::getApplicationEnvId, applicationEnvId);
        List<ApplicationItem> applicationItems = applicationItemMapper.selectList(lambdaQueryWrapper);
        return applicationItems;
    }


    public void updateApplicationItem(ApplicationItemRequest applicationItemRequest) {
        ApplicationItem applicationItem = new ApplicationItem();
        BeanUtils.copyProperties(applicationItemRequest, applicationItem);
        applicationItemMapper.updateById(applicationItem);
    }


    public void delApplicationItem(Long applicationItemId) {
        applicationItemMapper.deleteById(applicationItemId);
    }


    public MyPage<ApplicationItemVo> getCommonItem(Long applicationId, MyPage myPage) {
        return getApplicationEnvItem(applicationId, getCommonEnvId(applicationId), myPage);
    }

    public List<ApplicationItem> getApplicationCommonItem(Long applicationId, Long applicationEnvId) {
        return getApplicationEnvItem(applicationId, getCommonEnvId(applicationId));
    }


    public Long getCommonEnvId(Long applicationId) {
        ApplicationEnv applicationEnv = applicationEnvService.getApplicationEnvByName(applicationId, applicationEnvService.getCommonEnvName());
        return applicationEnv.getApplicationEnvId();
    }

}

